package com.itheima.service.impl;

import com.itheima.mapper.MemberMapper;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;


@Service
public class MemberServiceImpl implements MemberService {

        @Autowired
        private MemberMapper memberMapper;

    //根据月份获取会员数量
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            //  转换为当月的第一天
            LocalDate inputDate = LocalDate.parse(month + "-01");
            // 获取该月的最后一天
            LocalDate lastDayOfMonth = inputDate.withDayOfMonth(inputDate.lengthOfMonth());


            // 查询该月最后一天的会员数量
            Integer count = memberMapper.findMemberCountByMonth(lastDayOfMonth.toString());
            memberCount.add(count);
        }
        return memberCount;
    }
}
